<template>
    <div class="tinymce-editor" style="min-height: 200px;">
        <!-- <div id="editor"></div> -->
        <Editor v-model="myValue" 
                :init="init"
                :disabled="disabled"
                @onClick="onClick">
        </Editor>
    </div>
</template>

<script>
    import tinymce from 'tinymce/tinymce'
    import Editor from '@tinymce/tinymce-vue'
    import 'tinymce/icons/default/icons'
    import 'tinymce/themes/silver'
    import 'tinymce/plugins/image';
    import 'tinymce/plugins/media';
    import 'tinymce/plugins/table';
    import 'tinymce/plugins/lists';
    import 'tinymce/plugins/advlist';
    import 'tinymce/plugins/contextmenu';
    import 'tinymce/plugins/wordcount';
    import 'tinymce/plugins/colorpicker';
    import 'tinymce/plugins/textcolor';
    import 'tinymce/plugins/preview'
    import 'tinymce/plugins/print'
    import 'tinymce/plugins/code'
    import 'tinymce/plugins/fullscreen'
    import 'tinymce/plugins/imagetools'
    import 'tinymce/plugins/link'
    import 'tinymce/plugins/pagebreak'
    // import 'tinymce/plugins/charmap'
    import 'tinymce/plugins/anchor'
    import 'tinymce/plugins/hr'
    // import 'tinymce/plugins/lineheight'
    // import 'tinymce/plugins/indent2em'
    import '@/plugins/tinymce/lineheight/index.js'
    import '@/plugins/tinymce/indent2em/index.js'
    import 'tinymce/plugins/emoticons'
    import 'tinymce/plugins/autosave'
    let vm = this
    export default {
        name: "Tinymce",
        components: {
            Editor
        },
        props: {
            //传入一个value，使组件支持v-model绑定
            value: {
                type: String,
                default: ''
            },
            disabled: {
                type: Boolean,
                default: false
            },

            plugins: {
                type: [String, Array],
                default: 'lists advlist  image imagetools media table  textcolor wordcount contextmenu'+
                ' preview paste hr emoticons  autosave fullscreen print code  lineheight indent2em fullscreen link pagebreak  anchor'
            },
            toolbar1: {
                type: [String, Array],
                default: 'code undo redo  cut  forecolor backcolor bold italic underline strikethrough link '+
                ' alignleft aligncenter alignright alignjustify  bullist numlist blockquote subscript superscript removeformat   '
            },
            toolbar2:{
                type: [String, Array],
                default: 'formatselect  fontselect fontsizeselect table image media emoticons hr pagebreak  print preview  indent2em lineheight  anchor fullscreen'

            },
            images_upload: {
                type: Boolean,
                default: true
            }

        },
        data() {
            return {
                //初始化配置
                init: {
                    language_url: '/tinymce/langs/zh_CN.js',// 语言包的路径
                    language: 'zh_CN',//语言
                    skin_url: '/tinymce/skins/ui/oxide',// skin路径
                    height: 334,//编辑器高度
                    plugins: this.plugins,
                    // toolbar: this.toolbar,
                    toolbar1: this.toolbar1,
                    toolbar2: this.toolbar2,
                    toolbar_mode: 'wrap',
                    fontsize_formats: "12pt 14pt 16pt 18pt 20pt 22pt 24pt", // 第二步
                    statusbar: false, // 显示隐藏状态栏
                    branding: false,//是否禁用“Powered by TinyMCE”
                    menubar: false,//顶部菜单栏显示
                    paste_data_images: this.images_upload, //粘贴图片
                    //此处为图片上传处理函数，这个直接用了base64的图片形式上传图片，
                    //如需ajax上传可参考https://www.tiny.cloud/docs/configure/file-image-upload/#images_upload_handler
                    // images_upload_handler: (blobInfo, success) => {
                    //     const img = 'data:image/jpeg;base64,' + blobInfo.base64();
                    //     // console.log(failure, blobInfo)
                    //     success(img)
                        
               
                    // },
                    images_upload_url: '/api/api/general/upload',
                    images_upload_handler: function (blobInfo, succFun, failFun) {
                        var xhr, formData;
                        var file = blobInfo.blob();//转化为易于理解的file对象
                        xhr = new XMLHttpRequest();
                        xhr.withCredentials = false;
                        xhr.open('POST', '/api/api/general/upload_edit_image');
                        xhr.onload = function() {
                            var json;
                            if (xhr.status != 200) {
                                failFun('HTTP Error: ' + xhr.status);
                                return;
                            }
                            json = JSON.parse(xhr.responseText);
                            // if (!json || typeof json.location != 'string') {
                            //     failFun('Invalid JSON: ' + xhr.responseText);
                            //     return;
                            // }
                            succFun(vm.utils.setCdnUrl() + json.data.filePathName);
                        };
                        formData = new FormData();
                        formData.append('file', file, file.name );//此处与源文档不一样
                        formData.append('type', 'article' );
                        xhr.send(formData);
                    }

                },
                myValue: this.value
            }
        },
        created (){
            vm = this
        },
        mounted() {
            tinymce.init({})     
        },
        methods: {
            //添加相关的事件，可用的事件参照文档=> https://github.com/tinymce/tinymce-vue => All available events
            //可以添加一些自己的自定义事件，如清空内容
            clear() {
                this.myValue = ''
            }
        },
        watch: {
            value(newValue) {
                this.myValue = newValue
            },
            myValue(newValue) {
                this.$emit('input', newValue)
            }
        }
    }

</script>

<style lang="scss">
.tox.tox-silver-sink.tox-tinymce-aux{
    z-index: 5000
}
.tinymce-editor{
    .tox.tox-tinymce{
        border-radius: 8px;
    }
}

</style>
